//
//  YDYTopicPictureView.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 2016/8/7.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYTopicPictureView.h"
#import <UIImageView+WebCache.h>
#import "YDYTopic.h"
#import <AFNetworking.h>
#import <DALabeledCircularProgressView.h>
#import "YDYSeeBigPicViewController.h"
@interface YDYTopicPictureView()
@property (weak, nonatomic) IBOutlet UIButton *showBigBtn;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@end
@implementation YDYTopicPictureView

-(void)awakeFromNib{
     [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone;
}
-(void)setTopic:(YDYTopic *)topic{
    _topic = topic;
    
    AFNetworkReachabilityStatus netWorkStatus = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
    
    self.progressView.hidden = YES;

    [self.imgView sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:self.topic.large_img] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressView.hidden = NO;
        CGFloat progress = receivedSize * 1.0 /expectedSize;
        self.progressView.progress = progress;
        
        self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.2f",progress];
        self.progressView.progressLabel.textColor = [UIColor darkGrayColor];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden = YES;
    }];
    //为了防止cell重用的时候图片重复出现，在这里根据网络环境清空图片
    switch (netWorkStatus) {
        case AFNetworkReachabilityStatusNotReachable:
            self.imgView.image = nil;
            break;
        case AFNetworkReachabilityStatusUnknown:
            NSLog(@"你处在不知名网络");
            break;
        default:
            break;
    }
    //gif
        //  1.  if (self.topic.large_img.lowercaseString hasSuffix:@"gif")
        //  2.  if (self.topic.large_img.pathExtension.lowercaseString isEqualToString:@"gif")
    //判断是不是gif
    self.gifView.hidden = !self.topic.is_gif;
    //判断是不是大图
    if (self.topic.isBigPicture && !self.topic.is_gif) {
        //设置图片显示格式
        self.imgView.contentMode = UIViewContentModeTop;
        self.imgView.clipsToBounds = YES;
    }else{
        self.imgView.contentMode =  UIViewContentModeScaleToFill;
        self.imgView.clipsToBounds = NO;
    }
    self.showBigBtn.hidden = !self.topic.isBigPicture;
    
    
}
- (IBAction)showBigBtnClick:(id)sender {
    YDYSeeBigPicViewController *seeBigVC = [[YDYSeeBigPicViewController alloc]init];
    seeBigVC.topic = self.topic;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigVC animated:YES completion:nil];
}


@end
